package com.service.impl;

import com.bean.Enterprise;
import com.bean.Flower;
import com.bean.Page;
import com.dao.FlowerDao;
import com.dao.impl.FlowerDaoImpl;
import com.service.FlowerService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class FlowerServiceImpl implements FlowerService {

    private FlowerDao flowerDao=new FlowerDaoImpl();

    @Override
    public Page<Flower> page(int pageNo, int pageSize) {
        Page<Flower>page=new Page<Flower>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount=flowerDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //求当前页数据开始的索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Flower> items=flowerDao.queryForPageItem(begin,pageSize);
        //设置当前页数据
        page.setItem(items);
        return page;
    }

    @Override
    public Page<Flower> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Flower>page=new Page<Flower>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);

        //求总记录数
        Integer pageTotalCount=flowerDao.queryForPageTotalCountByPrice(min,max);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //求当前页数据开始的索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Flower>items=flowerDao.queryForPageItemByPrice(begin,pageSize,min,max);
        //设置当前页数据
        page.setItem(items);

        return page;
    }

    @Override
    public Page<Flower> pageByName(int pageNo, int pageSize, String flower_name) {
        Page<Flower>page=new Page<Flower>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount=flowerDao.queryForPageTotalCountByName(flower_name);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //求当前页数据开始的索引
        int begin=(page.getPageNo()-1)*pageSize;

        //求当前页数据min,max
        List<Flower>items=flowerDao.queryForPageItemByName(begin,pageSize,flower_name);
        //设置当前页数据
        page.setItem(items);
        return page;
    }

    @Override
    public Flower queryFlowerByFlowerid(int id) {
        return flowerDao.queryFlowerByFlowerid(id);
    }
    @Override
    public List<Map<String, Object>> queryFlowerid(int flowerid) {
        return flowerDao.listquery(flowerid);
    }

    @Override
    public Page<Flower> pageByEnterpriseid(int pageNo, int pageSize, String enterpriseid) {
        Page<Flower>page=new Page<Flower>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount=flowerDao.queryForPageTotalCountByEnterpriseid(enterpriseid);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //求当前页数据开始的索引
        int begin=(page.getPageNo()-1)*pageSize;

        //求当前页数据min,max
        List<Flower>items=flowerDao.queryForPageItemByEnterpriseid(begin,pageSize,enterpriseid);
        //设置当前页数据
        page.setItem(items);
        return page;
    }

    @Override
    public void updateFlower(Flower flower) {
        flowerDao.updateFlower(flower);
    }

    @Override
    public void addFlower(Flower flower) {
        flowerDao.addFlower(flower);
    }

    @Override
    public void deleteFlowerByFlowerid(int flowerid) {
        flowerDao.deleteFlowerByFlowerid(flowerid);
    }

    @Override
    public Page<Flower> queryByFclassid(int pageNo, int pageSize, Integer fclassid) {
        Page<Flower>page=new Page<Flower>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount=flowerDao.queryForPageTotalCountByFclassid(fclassid);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //求当前页数据开始的索引
        int begin=(page.getPageNo()-1)*pageSize;

        //求当前页数据min,max
        List<Flower>items=flowerDao.queryForPageItemByFclassid(begin,pageSize,fclassid);
        //设置当前页数据
        page.setItem(items);
        return page;
    }

    @Override
    public List<Flower> queryBySales() {
        return flowerDao.queryBySales();
    }

    @Override
    public int updateById(Integer flowerid, String flower_name, BigDecimal price, String introduce, Integer stock, Integer fclassid) {
        return flowerDao.updateById(flowerid,flower_name,price,introduce,stock,fclassid);
    }

    @Override
    public void insertFlower(String flower_name, BigDecimal price, String introduce, Integer stock, Integer fclassid, String enterpriseid, String flower_phoro,Integer sales) {
        flowerDao.insertFlower(flower_name,price,introduce,stock,fclassid,enterpriseid,flower_phoro,sales);
    }

    @Override
    public void updateflowerPhoto(Integer flowerid, String enterpriseid, String flower_photo) {
        flowerDao.updatePhoto(flowerid,enterpriseid,flower_photo);
    }

    @Override
    public List<Map<String, Object>> queryByName(String content) {
        return flowerDao.queryByName(content);
    }

    @Override
    public List<Flower> queryByPrice(BigDecimal price) {
        return flowerDao.queryByPrice(price);
    }
}
